package com.pokeoffice.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SpringLayout;

import com.pokeoffice.calendar.Visit;
import com.pokeoffice.client.Client;
import com.pokeoffice.client.Session;
import com.pokeoffice.database.Control;
import javax.swing.ScrollPaneConstants;

public class VisitScreen extends JPanel {

	private static final long serialVersionUID = -2156895683451841467L;
	private Session session;
	private Vector<Visit> visits;

	public VisitScreen(final Session session) {
		this.session = session;
		SpringLayout springLayout = new SpringLayout();
		setLayout(springLayout);

		final JComboBox comboBox = new JComboBox();
		springLayout.putConstraint(SpringLayout.SOUTH, comboBox, -350,
				SpringLayout.SOUTH, this);
		

		final JList list = new JList();
		comboBox.setModel(new DefaultComboBoxModel(new String[] { "2011",
				"2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019",
				"2020" }));
		add(comboBox);

		update(list);

		JScrollPane scrollPane = new JScrollPane(list);
		springLayout.putConstraint(SpringLayout.WEST, scrollPane, 75,
				SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, scrollPane, 325,
				SpringLayout.WEST, this);
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		springLayout.putConstraint(SpringLayout.NORTH, scrollPane, 50,
				SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.SOUTH, scrollPane, -50,
				SpringLayout.SOUTH, this);
		add(scrollPane);
		springLayout.putConstraint(SpringLayout.WEST, comboBox, 100,
				SpringLayout.EAST, scrollPane);
		scrollPane.setViewportView(list);
		
		final JComboBox comboBox_1 = new JComboBox();
		springLayout.putConstraint(SpringLayout.NORTH, comboBox_1, 6,
				SpringLayout.SOUTH, comboBox);
		springLayout.putConstraint(SpringLayout.WEST, comboBox_1, 0,
				SpringLayout.WEST, comboBox);
		comboBox_1.setModel(new DefaultComboBoxModel(new String[] { "01", "02",
				"03", "04", "05", "06", "07", "08", "09", "10", "11", "12" }));
		add(comboBox_1);

		final JComboBox comboBox_2 = new JComboBox();
		springLayout.putConstraint(SpringLayout.NORTH, comboBox_2, 6,
				SpringLayout.SOUTH, comboBox_1);
		springLayout.putConstraint(SpringLayout.WEST, comboBox_2, 0,
				SpringLayout.WEST, comboBox);
		comboBox_2.setModel(new DefaultComboBoxModel(new String[] { "01", "02",
				"03", "04", "05", "06", "07", "08", "09", "10", "11", "12",
				"13", "14", "15", "16", "17", "18", "19", "20", "21", "22",
				"23", "24", "25", "26", "27", "28", "29", "30", "31" }));
		add(comboBox_2);

		JButton btnRequest = new JButton("Request");
		btnRequest.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				Visit vis = new Visit(0, session.user.getID(), 0, 0, 0, comboBox.getSelectedItem() + "-" + comboBox_1.getSelectedItem() + "-" + comboBox_2.getSelectedItem(), " ");
				Control ctrl = Control.get();
				ctrl.createVisit(ctrl.connect("root", "root"), vis);
				update(list);
			}
		});
		add(btnRequest);
		
		springLayout.putConstraint(SpringLayout.EAST, btnRequest, 0,
				SpringLayout.EAST, comboBox);
		springLayout.putConstraint(SpringLayout.NORTH, btnRequest, 25,
				SpringLayout.SOUTH, comboBox_2);
		
		JLabel lblYear = new JLabel("Year");
		lblYear.setForeground(Color.WHITE);
		springLayout.putConstraint(SpringLayout.NORTH, lblYear, 5,
				SpringLayout.NORTH, comboBox);
		springLayout.putConstraint(SpringLayout.EAST, lblYear, -8,
				SpringLayout.WEST, comboBox);
		add(lblYear);

		JLabel lblMonth = new JLabel("Month");
		lblMonth.setForeground(Color.WHITE);
		springLayout.putConstraint(SpringLayout.NORTH, lblMonth, 5,
				SpringLayout.NORTH, comboBox_1);
		springLayout.putConstraint(SpringLayout.EAST, lblMonth, -8,
				SpringLayout.WEST, comboBox_1);
		add(lblMonth);

		JLabel lblDay = new JLabel("Day");
		lblDay.setForeground(Color.WHITE);
		springLayout.putConstraint(SpringLayout.NORTH, lblDay, 5,
				SpringLayout.NORTH, comboBox_2);
		springLayout.putConstraint(SpringLayout.EAST, lblDay, -8,
				SpringLayout.WEST, comboBox_2);
		add(lblDay);

		JButton btnRemove = new JButton("Remove");
		springLayout.putConstraint(SpringLayout.WEST, btnRemove, 67,
				SpringLayout.EAST, scrollPane);
		btnRemove.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				Control ctrl = Control.get();
				Object[] arr = list.getSelectedValues();
				for (Object o : arr) {
					visits.remove(o);
					ctrl.deleteVisit(ctrl.connect("root", "root"), ((Visit) o).getID());
				}
				update(list);
			}
		});
		springLayout.putConstraint(SpringLayout.SOUTH, btnRemove, -25,
				SpringLayout.NORTH, comboBox);
		add(btnRemove);

		ImagePanel panel = new ImagePanel(Client.oak);
		springLayout.putConstraint(SpringLayout.NORTH, panel, 0,
				SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.WEST, panel, 0,
				SpringLayout.WEST, this);
		add(panel);

		Dimension size = new Dimension(Client.WIDTH, Client.HEIGHT);
		setPreferredSize(size);
		setMinimumSize(size);
		setMaximumSize(size);
		setSize(size);
	}

	public void update(JList list) {
		Control ctrl = Control.get();
		long uid = session.user.getID();
		visits = ctrl.readVisits(ctrl.connect("root", "root"), uid);
		list.setListData(visits);
	}

}
